﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SMS.Client
{
    public partial class NewClient : System.Web.UI.Page
    {
        SMS.Business.Customer.CustomerBussiness CustBussiness;
        SMS.DAL.Customer UpdatedCust;
        //New Mode = 0 , Update Mode =1
        private bool IsUpdate = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurrentUser"] == null)
                Response.Redirect(@"~\Account\Login.aspx");

            CustBussiness = new SMS.Business.Customer.CustomerBussiness();
            if (Request.QueryString["ClientID"] != null)
            {
                IsUpdate = true;
                if (!IsPostBack)
                {
                    UpdatedCust = CustBussiness.GetByID((int.Parse(SMS.Helper.QueryEncoder.Decrypt(Request.QueryString["ClientID"].ToString()))));
                    txtAdress.Text = UpdatedCust.Address;
                    txtCredit.Text = UpdatedCust.Credit.ToString();
                    txtCredit.Enabled = false;
                    rdioMoneyType.Enabled = false;
                    if (UpdatedCust.Credit > 0)
                        rdioMoneyType.Items.FindByValue("Credit").Selected = true;
                    else
                        rdioMoneyType.Items.FindByValue("Debit").Selected = true;

                    txtDiscountRate.Text = UpdatedCust.DiscountPercentage.ToString();
                    txtEmail.Text = UpdatedCust.Email;
                    txtMobile.Text = UpdatedCust.Mobile;
                    txtName.Text = UpdatedCust.Name;
                    txtPhone.Text = UpdatedCust.Phone;
                    btnCreate.Text = "تحديث";
                }
            }
            else
                rdioMoneyType.Items.FindByValue("Credit").Selected = true;

        }

        protected void CreateUserButton_Click(object sender, EventArgs e)
        {
            if (!IsUpdate)
            {
                try
                {
                    CustBussiness.Insert(new DAL.Customer()
                    {
                        Address = txtAdress.Text,
                        Credit = rdioMoneyType.SelectedValue == "Credit" ? decimal.Parse(txtCredit.Text) : decimal.Parse(txtCredit.Text) * -1,
                        Email = txtEmail.Text,
                        Mobile = txtMobile.Text,
                        Phone = txtPhone.Text,
                        DiscountPercentage = double.Parse(txtDiscountRate.Text),
                        Name = txtName.Text
                    });
                    lblResult.Text = "تم اضافة العميل بنجاح";
                }
                catch (Exception ex)
                {

                }

            }
            else
            {
                try
                {
                    CustBussiness.Update(new DAL.Customer()
                    {
                        ID = UpdatedCust.ID,
                        Address = txtAdress.Text,
                        Credit = rdioMoneyType.SelectedValue == "Credit" ? decimal.Parse(txtCredit.Text) : decimal.Parse(txtCredit.Text) * -1,
                        Email = txtEmail.Text,
                        Mobile = txtMobile.Text,
                        Phone = txtPhone.Text,
                        DiscountPercentage = double.Parse(txtDiscountRate.Text),
                        Name = txtName.Text
                    });
                    lblResult.Text = "تم تحديث العميل بنجاح";
                }
                catch (Exception ex)
                {

                }

            }
        }
    }
}